<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * LoginForm is the model behind the login form.
 */
class LoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = false; 

    private $_user = false;

    
    
    
    
    
    
    
    /**
     * 记住登录的天数
    */
    const rememberMeDay=30;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
          [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
          
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
       
     
            $user = $this->getUser(); 
            if (!$user || !$user->validatePassword($this->password)) {
                Log::addLoginFail($this->username, $this->password);
                $this->addError($attribute, '用户或者密码错误');
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     * @return boolean whether the user is logged in successfully
     */
    public function login()
    {   
         
        if(empty($this->password)){
            return false;
        }
        if ($this->validate()) {
            $this->WriteUptime();
            \app\models\Log::addLoginPass();
            return $this->getUser()->login();
          //return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*self::rememberMeDay : 0);
          
        } else {
            return false;
        }
    }
    
    /**
     * 写入密码更新时间到cookie里面,以便实现更改密码后使之登录状态失效
    */
    private function WriteUptime(){
         $cookies = Yii::$app->response->cookies;
        // 在要发送的响应中添加一个新的cookie
        $cookies->add(new \yii\web\Cookie([
            'name' =>'wuptime',
            'value' =>  $this->_user->password_time,
        ]));
    }


    
    
    /***/
    public function getIdUser($id){
        if ($this->_user === false) {
            $this->_user = User::findById($id);       
        }
        return $this->_user;
    }

    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    public function getUser()
    {
          
        if ($this->_user === false) {
         //   $this->_user = User::findByUsername($this->username);
           $this->_user = User::find()
           ->where(
               'username LIKE :username AND role_type=:role_type',   /// 
              [ ':username'=>  $this->username,
              ':role_type'=>  User::role_type_admin
               ])->one(); 
        }

        return $this->_user;
    }
    
    
    
    
      /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'username' => '用户名', 
            'password'=>'密码',
            'rememberMe'=>'记住登录'
        ];
    }
}
